<!doctype html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="./css/aui.css" />
    <link rel="stylesheet" type="text/css" href="./css/aui-slide.css" />
    <link rel="stylesheet" type="text/css" href="./css/font-awesome.css" />
    <link rel="stylesheet" type="text/css" href="./css/style.css" />
    <script type="text/javascript" src="./script/jquery.min.js"></script>
    <script type="text/javascript" src="./script/doT.min.js"></script>
    <script type="text/javascript" src="./script/base.js"></script>
    <script type="text/javascript" src="./script/api.js"></script>
</head>

<body>
    <header class="header header-f" style="padding-top:2rem; height:4rem;">
        <div class="header-l aui-pull-left" onclick="closeWin()">
            <i class="fa fa-angle-left"></i>
        </div>
        <form class="search" action="javascript:search()">

        <div class="search">
            <label>
            <span class="aui-iconfont aui-icon-search"></span>
            <input type="text" placeholder="请输入商品名称">
        </label>
        </div>
        <div class="header-r aui-pull-right" tapmode onclick="cart()">
            <i class="fa fa-shopping-cart"></i>
        </div>
    </header>
    <ul class="goods-nav header-f" style="top:4rem;">
        <li onclick='getData()' id='default'>默认</li>
        <li onclick='getData("sale")' id='sale'>销量<i class="fa fa-caret-down"></i></li>
        <li onclick='getData("new")' id='new'>新品<i class="fa fa-caret-down"></i></li>
        <li onclick='getData("price")' id='price'>价格<i class="fa fa-caret-down"></i></li>
    </ul>
    <div style="height:6.8rem;"></div>
  </form>

    <div id="list">

    </div>

</body>
<script type="text/javascript" src="./script/api.js"></script>
<script type="text/template" id="template">
    <div class="index-goods">
        <ul>
            {{~it.result.data:value:index}}
            <li onclick="buy_detail({{=value.mall_id}})">
                <img src="{{=value.image}}">
                <dl>
                    <dt>{{=value.title}}</dt>
                    <dd>￥{{=value.market_price}}<span>立即够买</span></dd>
                </dl>
            </li>
            {{~}}
        </ul>
    </div>
</script>
<script type="text/javascript">
    var index;
    apiready = function() {
        index = api.pageParam.index;
        getData();
        $('#default').addClass('active');
    }

    function search() {
        var text = $('input').val();
        getData('',text);

    }


    function getData(order,text='') {
        $('li').removeClass();
        $('#' + order).addClass('active');

        api.ajax({
            url: webAddress + '/a/mall/List/index',
            method: 'get',
            data: {
                values: {
                    id: index,
                    limit: 50,
                    order: order,
                    keyword: text
                },
            },
            headers: {
                TOKEN: token
            }
        }, function(ret, err) {
            if (ret) {
                // console.log(JSON.stringify(ret));

                var template = $api.byId('template');
                var dotFn = doT.template(template.innerHTML);
                var html = dotFn(ret);
                var list = $api.byId('list');
                $api.html(list, html);
            } else {
                alert(JSON.stringify(err));
            }
        });


    }

    function buy_detail(index) {
        api.openWin({
            name: 'goods_details',
            url: './goods_details.html',
            pageParam: {
                id: index
            }
        });
    }


    function closeWin() {
        api.closeWin({});
    }
</script>

</html>
